package org.itboys.baseobject.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.baseobject.entity.KeyValuesConfig;
import org.itboys.baseobject.service.KeyValuesConfigService;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * KEY-List<String> value配置控制层
 * @author weisky
 *
 */
@RestController 
@RequestMapping("/kValuesConfig") 
public class KeyValuesConfigController extends BaseController{

	@Autowired
	private KeyValuesConfigService keyValuesConfigService;
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		List<KeyValuesConfig> kvc = keyValuesConfigService.list(param);
		Long count = keyValuesConfigService.count(param);
		JsonPageUtils.renderJsonPage(count, kvc, response);
	}
	
	@RequestMapping("/getById")
	public void getById(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, keyValuesConfigService.getById(id));
	}
	
	@RequestMapping("/save")
	public void save(KeyValuesConfig kvc, HttpServletRequest request, HttpServletResponse response){
		try{
			keyValuesConfigService.saveOrUpdate(kvc);
			AjaxUtils.renderJson(response, 1);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("KeyValuesConfigController.save error", e);
			AjaxUtils.renderJson(response, -1);
		}
	}
	
	@RequestMapping("/delete")
	public void delete(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		try{
			keyValuesConfigService.delete(id);
			AjaxUtils.renderJson(response, 1);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("KeyValuesConfigController.delete error", e);
			AjaxUtils.renderJson(response, -1);
		}
	}
	
}
